////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "ShadowCasterEntity.hpp"

#include "../Hull/TextureHull.hpp"

#include "../Light/LightSystem.hpp"

#include <SmegTools/Debug/DebugNew.hpp>

namespace smeg {
    namespace light {
        
////////////////////////////////////////////////////////////
/// Default Constructor
////////////////////////////////////////////////////////////
ShadowCasterEntity::ShadowCasterEntity() :
    core::MovableEntity(),
    m_pHull( NULL )
{
}
    
////////////////////////////////////////////////////////////
/// Constructor
////////////////////////////////////////////////////////////
ShadowCasterEntity::ShadowCasterEntity( sf::RenderWindow* _pRW,
                        const sf::Vector2f& _Center,
                        const sf::Vector2f& _Velocity,
                        float _fMass,
                        float _fMaxSpeed,
                        float _fAngleDeg,
                        bool _bIsStatic ) :
    core::MovableEntity( _pRW, _Center, _Velocity, _fMass, _fMaxSpeed, _fAngleDeg, _bIsStatic ),
    m_pHull( NULL )
{
}
    
////////////////////////////////////////////////////////////
/// Constructor with a sprite
////////////////////////////////////////////////////////////
ShadowCasterEntity::ShadowCasterEntity( sf::RenderWindow* _pRW,
                        const std::string& _TextureName,
                        const sf::Vector2f& _Center,
                        const sf::Vector2f& _Velocity,
                        float _fMass,
                        float _fMaxSpeed,
                        float _fAngleDeg,
                        bool _bIsStatic ) :
    core::MovableEntity( _pRW, _TextureName, _Center, _Velocity, _fMass, _fMaxSpeed, _fAngleDeg, _bIsStatic ),
    m_pHull( NULL )
{
}
    
////////////////////////////////////////////////////////////
/// Copy Constructor
////////////////////////////////////////////////////////////
ShadowCasterEntity::ShadowCasterEntity( const ShadowCasterEntity& _Copy ) :
    core::MovableEntity( _Copy ),
    m_pHull( NULL )
{
    if ( _Copy.m_pHull ) {
        m_pHull = static_cast<Hull*>(_Copy.m_pHull->Clone());
        m_pHull->SetParent( this );
        light::LightSystem::GetInstance()->AddHull( m_pHull );
    }
}
    
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
ShadowCasterEntity::~ShadowCasterEntity()
{
    if ( m_pHull ) {
        light::LightSystem::GetInstance()->RemoveHull( m_pHull );
    }
}

/*********************** Accessors ************************/
bool ShadowCasterEntity::SetSprite( const std::string& _SpriteFilename ) {
    if ( core::MovableEntity::SetSprite( _SpriteFilename ) ) {
        RemoveHull();
        m_pHull = new TextureHull( _SpriteFilename, m_Center, m_fAngleDeg, m_bIsStatic, this );

        return true;
    }
    
    return false;
}

void ShadowCasterEntity::SetHull( light::Hull* _hull ) {
    m_pHull = _hull;
    m_pHull->SetCenter( m_Center.x, m_Center.y );
    m_pHull->SetAngleRad( m_fAngleRad );
    m_pHull->SetParent( this );

    light::LightSystem::GetInstance()->AddHull( m_pHull );
}

void ShadowCasterEntity::RemoveHull() {
    if ( m_pHull ) {
        light::LightSystem::GetInstance()->RemoveHull( m_pHull );
        m_pHull = NULL;
    }
}

void ShadowCasterEntity::SetCenter( float _fX, float _fY ) {
    core::MovableEntity::SetCenter( _fX, _fY );

    if ( m_pHull ) {
        m_pHull->SetCenter( m_Center.x, m_Center.y );
    }
}

void ShadowCasterEntity::Move( float _fX, float _fY ) {
    core::MovableEntity::Move( _fX, _fY );

    if ( m_pHull ) {
        m_pHull->SetCenter( m_Center.x, m_Center.y );
    }
}

void ShadowCasterEntity::SetAngleRad( float _fAngleRad ) {
    core::MovableEntity::SetAngleRad( _fAngleRad );
    
    if ( m_pHull ) {
        m_pHull->SetAngleRad( m_fAngleDeg );
    }
}

void ShadowCasterEntity::RotateRad( float _fAngleRad ) {
    core::MovableEntity::RotateRad( _fAngleRad );
    
    if ( m_pHull ) {
        m_pHull->SetAngleRad( m_fAngleDeg );
    }
}

void ShadowCasterEntity::SetStatic( bool _bIsStatic ) {
    core::MovableEntity::SetStatic( _bIsStatic );

    if ( m_pHull ) {
        light::LightSystem::GetInstance()->SetStatic( m_pHull, _bIsStatic );
    }
}

#ifdef _DEBUG
void ShadowCasterEntity::DebugRender( const sf::Color& _Color ) const {
    if ( m_pHull ) {
        if ( DEBUG_DebugRender ) {
            m_pHull->DEBUG_DebugRender = true;
            m_pHull->DebugRender( _Color );
        } else {
            m_pHull->DEBUG_DebugRender = false;
        }
    }
}
#endif // _DEBUG

    } // namespace light
} // namespace smeg